package ua.org.extjsbuilder.parser.runtime;

import java.util.Arrays;
import java.util.List;

import ua.org.extjsbuilder.parser.IInstruction;
import ua.org.extjsbuilder.parser.runtime.instructions.BinaryOperator;
import ua.org.extjsbuilder.parser.runtime.instructions.Declare;
import ua.org.extjsbuilder.parser.runtime.instructions.GetEntity;

public class ExpressionsInterpreterTestCase extends AbstractInterpreterTestCase {

	@Override
	protected String getMockPkg() {
		return "ua/org/extjsbuilder/parser/runtime/expressions/";
	}

	public void testBaseMath() throws Exception {
		Class<?>[] result = new Class[9];
		Arrays.fill(result, BinaryOperator.class);
		List<IInstruction> operators = parseFile("basemath.js",result);
		
		BinaryOperator toTest = (BinaryOperator)operators.get(0);
		verifyOperator(toTest,BinaryOperator.EQ,Declare.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.PLUS,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(1);
		verifyOperator(toTest,BinaryOperator.EQ,Declare.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.PLUS,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(2);
		verifyOperator(toTest,BinaryOperator.EQ,Declare.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.PLUS,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(3);
		verifyOperator(toTest,BinaryOperator.EQ,GetEntity.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.MINUS,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(4);
		verifyOperator(toTest,BinaryOperator.EQ,Declare.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.MULTIPLY,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(5);
		verifyOperator(toTest,BinaryOperator.EQ,Declare.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.MULTIPLY,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(6);
		verifyOperator(toTest,BinaryOperator.EQ,GetEntity.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.MULTIPLY,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(7);
		verifyOperator(toTest,BinaryOperator.EQ,GetEntity.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.DIVIDE,GetEntity.class,GetEntity.class);
		
		toTest = (BinaryOperator)operators.get(8);
		verifyOperator(toTest,BinaryOperator.EQ,GetEntity.class,BinaryOperator.class);
		toTest = (BinaryOperator)toTest.getSecondArg();
		verifyOperator(toTest,BinaryOperator.DIVIDE,GetEntity.class,GetEntity.class);
	}

	private void verifyOperator(BinaryOperator toTest, String type, Class<?> fisrt,
			Class<?> second) {
		assertEquals(type, toTest.getName());
		assertNotNull(toTest.getFirstArg());
		assertNotNull(toTest.getSecondArg());
		assertEquals(fisrt,toTest.getFirstArg().getClass());
		assertEquals(second,toTest.getSecondArg().getClass());
	}
}
